<!DOCTYPE html>
<html lang="zh-CN" style="height: 100%;overflow: hidden;">
<head>
    <meta charset="UTF-8">
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">

    <link href="./static/plugins/bootstrap/bootstrap.min.css" rel="stylesheet">
    <link href="./static/plugins/toastr/toastr.min.css" rel="stylesheet">
    <link href="./static/plugins/ladda/ladda-themeless.min.css" rel="stylesheet">

    <script type="text/javascript" src="./static/js/jquery-3.1.1.min.js"></script>
    <style>
        .pink {
            color: #afff96;
            font-weight: bold;
            font-style: normal;
            text-decoration: underline;
            font-size: 17px;
        }
    </style>
</head>
<body style="height:100%">
<div id="wrapper" v-cloak style="height: calc(100% - 1px)">
    <iframe src="home.html" frameborder="0" style="width:100%;height:100%" id="main"></iframe>
</div>

<script type="text/javascript" src="./static/plugins/toastr/toastr.min.js"></script>
<script type="text/javascript" src="./static/plugins/ladda/spin.min.js"></script>
<script type="text/javascript" src="./static/plugins/ladda/ladda.min.js"></script>
<script type="text/javascript" src="./static/plugins/ladda/ladda.jquery.min.js"></script>
<script type="text/javascript" src="./static/js/vue.min.js"></script>
<script type="text/javascript" src="./static/js/main.js"></script>
<script>
    var createAudio = function (src) {
        var audio = document.createElement('audio');
        audio.src = src;
        return audio;
    };

    var newInquiry = createAudio('http://ejp.zy52.cn/static/sound/newInquiry.mp3');
    var newQuote = createAudio('http://ejp.zy52.cn/static/sound/newQuote.mp3');
    var authOk = createAudio('http://ejp.zy52.cn/static/sound/authOk.mp3');
    var authFail = createAudio('http://ejp.zy52.cn/static/sound/authFail.mp3');
    var newOrder = createAudio('http://ejp.zy52.cn/static/sound/newOrder.mp3');
    var pendingVerify = createAudio('http://ejp.zy52.cn/static/sound/pendingVerify.mp3');
    var verifyFail = createAudio('http://ejp.zy52.cn/static/sound/verifyFail.mp3');
    var delivery = createAudio('http://ejp.zy52.cn/static/sound/delivery.mp3');
    var receive = createAudio('http://ejp.zy52.cn/static/sound/receive.mp3');
    var quoteModify = createAudio('http://ejp.zy52.cn/static/sound/quoteModify.mp3');

    var WebSocket_url = 'ws://122.114.73.217:9503';
    var socket;
    var lockReconnect = false, jump = null;
    var user = getItem('info');
    var heartCheck = {
        timeout: 5000, //心跳间隔
        timeoutObj: null,
        serverTimeoutObj: null,
        reset: function () {
            clearTimeout(heartCheck.timeoutObj);
            clearTimeout(heartCheck.serverTimeoutObj);
            return this;
        },
        start: function () {
            heartCheck.timeoutObj && clearTimeout(heartCheck.timeoutObj);
            heartCheck.serverTimeoutObj && clearTimeout(heartCheck.serverTimeoutObj);
            heartCheck.timeoutObj = setTimeout(function () {
                if (socket && socket.readyState === 1) {
                    socket.send("ping");
                    console.log(JSON.stringify('心跳中...'));
                    heartCheck.serverTimeoutObj = setTimeout(function () {
                        console.log(JSON.stringify('热重启心跳.'));
                        reconnect();
                    }, heartCheck.timeout + 2000);
                } else {
                    console.log(JSON.stringify('冷重启心跳.'));
                    lockReconnect = false;
                    reconnect();
                }
            }, heartCheck.timeout);
        }
    };

    var reconnect = function () {
        if (lockReconnect) {
            return false;
        }
        lockReconnect = true;
        jump && clearTimeout(jump);
        jump = setTimeout(function () {
            wsconnect();
            lockReconnect = false;
        }, 4000);
    };

    wsconnect = function () {
        try {
            socket = new WebSocket(WebSocket_url, 'p-' + user.id);
        } catch (e) {
            console.log('websocket error');
            return false;
        }
        socket.onmessage = function (msg) {
            if (msg.data != 'pong') {
                var response = JSON.parse(msg.data);
                var msgTips = '';
                switch (response.action) {
                    case 'newInquiry':
                        newInquiry.play();
                        msgTips = '<a data-url="inquiry_supply.html?id=' + response.inquiry_id + '">您有一条新的&nbsp;<em class="pink">询价单</em>&nbsp;，请注意查收！</a>';
                        break;
                    case 'newQuote':
                        newQuote.play();
                        msgTips = '<a data-url="inquiry.html?id=' + response.inquiry_id + '">您有一条新的&nbsp;<em class="pink">报价单</em>&nbsp;，请注意查收！</a>';
                        break;
                    case 'authFail':
                        authFail.play();
                        msgTips = '<a data-url="verify.html">您的会员认证未通过,请注意查收！</a>';
                        break;
                    case 'authOk':
                        authOk.play();
                        msgTips = '<a data-url="verify.html">您的会员认证已通过，请注意查收！</a>';
                        break;
                    case 'newOrder':
                        newOrder.play();
                        msgTips = '<a data-url="order_detail_provider.html?order_no=' + response.order_no + '">您有一条新的订单，请注意查收！</a>';
                        break;
                    case 'pendingVerify':
                        pendingVerify.play();
                        msgTips = '<a data-url="inquiry_audit.html?id=' + response.inquiry_id + '">您有一条报价待审核，请注意查收！</a>';
                        break;
                    case 'verifyFail':
                        verifyFail.play();
                        msgTips = '<a data-url="offer_detail.html?id=' + response.inquiry_id + '">您有一条报价审核未通过，请注意查收！</a>';
                        break;
                    case 'delivery':
                        delivery.play();
                        msgTips = '<a data-url="order_detail.html?order_no=' + response.order_no + '">您有订单已确认发货，请注意查收！</a>';
                        break;
                    case 'receive':
                        receive.play();
                        msgTips = '<a data-url="order_detail_provider.html?order_no=' + response.order_no + '">您有订单已确认收货，请注意查收！</a>';
                        break;
                    case 'quoteModify':
                        quoteModify.play();
                        msgTips = '<a data-url="inquiry_audit.html?id=' + response.inquiry_id + '">您有一条报价被更新，请注意查收！</a>';
                        break;
                }
                if (msgTips) {
                    toastr.info(msgTips, '', {
                        timeOut: 300000,
                        positionClass: "toast-bottom-right",
                        onclick: function () {
                            document.getElementById('main').src = $(event.currentTarget).find('a').data('url');
                        }
                    });
                }
            }
            heartCheck.reset().start();
        };
        socket.onerror = function () {
            reconnect();
        };
        socket.onclose = function () {
            reconnect();
        };
        socket.onopen = function () {
            heartCheck.reset().start();
        };
    };

    $.ajax({
        type: 'POST',
        url: api + 'user/home',
        success: function (data) {
            checkToken(data);
            user.company_id = data.data.company_id;
            user.company = data.data.company.name;
            user.avatar = data.data.avatar;
            user.id = data.data.id;
            user.is_root = data.data.is_root;
            user.name = data.data.name;
            user.phone = data.data.phone;
            user.type = data.data.type;
            user.uid = data.data.uid;
            user.logined_at = data.data.logined_at;
            user.gender = data.data.gender;
            setItem('info', user);
            wsconnect();
        },
    });
</script>
</body>
</html>
